

#include"Runlayer.h"
#include"Role.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

using namespace cocos2d;

bool Runlayer::init()
{
	if (!Layer::init())
	{
		return false;
	}

	auto keyLinster = EventListenerKeyboard::create();
	keyLinster->onKeyPressed = CC_CALLBACK_2(Runlayer::onKeyPressed, this); 
	keyLinster->onKeyReleased = CC_CALLBACK_2(Runlayer::onKeyReleased, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(keyLinster, this);

	return true;
}


void Runlayer::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
	Point nextPoint;
	switch (keyCode) {
	case EventKeyboard::KeyCode::KEY_CAPITAL_A:
	case EventKeyboard::KeyCode::KEY_A:
		m_pRole->spriteDirection = LEFT;
		nextPoint.setPoint(-4,0);
		break;
	case EventKeyboard::KeyCode::KEY_CAPITAL_D:
	case EventKeyboard::KeyCode::KEY_D:
		m_pRole->spriteDirection = RIGHT;
		nextPoint.setPoint(4, 0);
		break;
	case EventKeyboard::KeyCode::KEY_CAPITAL_W:
	case EventKeyboard::KeyCode::KEY_W:
		m_pRole->spriteDirection = UP;
		nextPoint.setPoint(0 ,4);
		break;
	case EventKeyboard::KeyCode::KEY_CAPITAL_S:
	case EventKeyboard::KeyCode::KEY_S:
		m_pRole->spriteDirection = DOWN;
		nextPoint.setPoint(0, -4);
		break;
	default:
		nextPoint.setPoint(0, 0);
		break;
	}
	m_pRole->walk(m_pRole->spriteDirection, nextPoint);
}

void Runlayer::onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event)
{
	m_pRole->stop();
}

